<template>
  <t-link
    theme="primary"
    @click="handleOpenDialog('update', { id: props.ids })"
  >
    分配公司
  </t-link>

  <!-- 自定义对话框 -->
  <custom-dialog
    ref="customDialogRef"
    v-slot="{ formData }"
    :api-map="{ update: assignCompany }"
    header="分配公司"
    @request-success="handleDialogRequestSuccess"
  >
    <t-form-item label="公司" name="companyId" :rules="[{ required: true, message: '请选择公司' }]">
      <t-select
        v-model:value="formData.companyId"
        :options="saleList"
        :keys="{ label: 'name', value: 'id' }"
        placeholder="请选择公司"
        clearable
        filterable
        @clear="formData.saleId = null"
      />
    </t-form-item>
  </custom-dialog>
</template>

<script setup lang="ts">
import { MessagePlugin } from 'tdesign-vue-next';
import { PropType, ref, useTemplateRef } from 'vue';

import { assignCompany } from '@/api/delivery/clue';
import { getAllSimpleUserlist, getUserListByRoleCode } from '@/api/system/user';
import CustomDialog from '@/components/custom/dialog/index.vue';
import { deliveryListCRUD } from '@/api/delivery/delivery';

// props
const props = defineProps({
  // 订单ids
  ids: {
    type: Array as PropType<number[]>,
    required: true,
  },

});
// emits
const emits = defineEmits(['requestSuccess']);
// 公司列表
const saleList = ref([]);
// 处理获取公司列表
const handleGetSaleList = () => {
  deliveryListCRUD.page({pageSize:-1})
    .then((data) => {
      saleList.value = data.list;
    })
    .catch(({ message }) => {
      MessagePlugin.error(message);
    });
};
handleGetSaleList()
// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');
// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  handleGetSaleList();
  customDialogRef.value.open(mode, params);
};
// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  emits('requestSuccess', result);
};
</script>
